/* eslint-disable array-callback-return,no-undef */
import React, { Component } from 'react';
import { Layout, Tabs, Form, Spin, Avatar } from 'antd';
import styles from '../../index.less';
import Toolbar from '../Common/ToolBar/ToolBarNew';
import * as commonFunc from '../Common/commonFunc';
import CommonBase from '../Common/CommonBase';
import CommonElementEvent from '../Common/CommonElementEvent';
import StaticEditTable from '../Common/CommonTable'; /* 可编辑表格 */
import CommonView from '../Common/CommonView';
import * as commonBusiness from '../Common/commonBusiness'; /* 单据业务功能 */
import * as commonConfig from '../../utils/config';

const { Header, Content } = Layout;
const { TabPane } = Tabs;

class PrintInfoComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
    };
    this.form = {}; /*   表单对象   */
  }
  /**   上传后执行函数    */
  handleUploadChange = (info, name) => {
    const { file } = info;
    if (file.response) {
      const sPicturePath = file.response.dataset.rows[0].savePathStr;
      const uploadInfo = {
        sPicturePath,
        sFileName: file.name,
      };
      const tableDataRow = this.props.onDataRowAdd(name, true);
      const { [`${name}Column`]: tableColumn, masterData, [`${name}Data`]: tableData } = this.props;
      tableColumn.forEach((slaveChild) => {
        const ckey = slaveChild.dataIndex;
        if (ckey === 'sProductNo' || ckey === 'sProductName') {
          tableDataRow[ckey] = masterData[ckey] !== undefined ? masterData[ckey] : '';
        } else if (ckey === 'iOrder') {
          tableDataRow[ckey] = '';
        } else {
          tableDataRow[ckey] = uploadInfo[ckey];
        }
      });
      tableData.push(tableDataRow);
      this.props.onSaveState({ [`${name}Data`]: tableData });
    }
  };
  /**   查看   */
  handleTableLook = (name) => {
    const { [`${name}SelectedRowKeys`]: selectedRowKeys, [`${name}Data`]: data } = this.props;
    if (selectedRowKeys === undefined || selectedRowKeys.length !== 1) {
      return message.warning('请选择一条数据进行查看');
    } else {
      const dataSelect = data.filter(item => item.sId === selectedRowKeys[0]);
      const { currentId } = this.state;
      const { token } = this.props.app;
      const { sPicturePath } = dataSelect[0];
      const previewUrl = `${commonConfig.server_host}file/download?sModelsId=${currentId}&token=${token}&savePathStr=${sPicturePath}`;
      this.props.onSaveState({ previewImage: previewUrl, previewVisible: true });
    }
  };
  /**   关闭弹窗    */
  handleModalCancel = () => {
    this.props.onSaveState({ previewVisible: false });
  };
  render() {
    const { pageLoading } = this.props;
    return (
      <div>
        <Spin spinning={pageLoading}>
          <div>
            <PrintComponent
              {...this.props}
              {...this.state}
              onUploadChange={this.handleUploadChange}
              onModalCancel={this.handleModalCancel}
              onDataRowLook={this.handleTableLook}
            />
          </div>
        </Spin>
      </div>
    );
  }
}
const PrintComponent = Form.create({
  mapPropsToFields(props) {
    const { masterData } = props;
    const obj = commonFunc.mapPropsToFields(masterData, Form);
    return obj;
  },
})((props) => {
  const { form, onReturnForm } = props;
  /*   回带表单   */
  onReturnForm(form);
  return (
    <Form>
      <Layout>
        <Header className={styles.header}>
          <Toolbar {...props} />
        </Header>
        <Layout className={styles.clayout}>
          <Content className={styles.content}>
            <CommonView {...props} />
            <div id="slaveTabs" className={styles.bShow}>
              <div>
                <Avatar src={props.imgSrc} />
              </div>
              <Tabs className={styles.slaveTabs}>
                <TabPane tab="图片文件" key={2}>
                  <StaticEditTable {...commonBusiness.getTableTypes('picFile', props)} />
                </TabPane>
                <TabPane tab="仓库" key={3}>
                  <StaticEditTable {...commonBusiness.getTableTypes('stock', props)} />
                </TabPane>
              </Tabs>
            </div>
          </Content>
        </Layout>
      </Layout>
    </Form>
  );
});

export default CommonBase(CommonElementEvent(PrintInfoComponent));

